package io.renren.service.impl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.IntDexUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.SeatPageUtils;
import io.renren.dao.SeatDao;
import io.renren.entity.SeatApplicationEntity;
import io.renren.entity.SeatEntity;
import io.renren.entity.SeatEslEntity;
import io.renren.entity.SysDictEntity;
import io.renren.param.SysDictQueryParam;
import io.renren.service.SeatApplicationService;
import io.renren.service.SeatEslService;
import io.renren.service.SeatService;
import io.renren.service.SysDictService;
import io.renren.vo.SeatVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("seatService")
public class SeatServiceImpl extends ServiceImpl<SeatDao, SeatEntity> implements SeatService {
    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private SeatDao seatDao;

    @Autowired
    private SeatEslService seatEslService;

    @Autowired
    private SeatApplicationService seatApplicationService;

    @Override
    public SeatPageUtils queryPage(Map<String, Object> params) {
        String state = null, city = null, floor = null, name = null, code = null, bookDate = null, startTime = null, endTime = null, compId = null, type = null;
        state = (String) params.get("state");
        city = (String) params.get("city");
        floor = (String) params.get("floor");
        code = (String) params.get("code");
        name = (String) params.get("name");
        compId = (String) params.get("compId");
        bookDate = (String) params.get("bookDate");
        startTime = (String) params.get("startTime");
        endTime = (String) params.get("endTime");
        type = (String) params.get("type");

        IPage<SeatEntity> page = this.page(
                new Query<SeatEntity>().getPage(params),
                new QueryWrapper<SeatEntity>()
                        .eq(state != null && StringUtils.isNotBlank(state), "state", state)
                        .eq(floor != null && StringUtils.isNotBlank(floor), "floor", floor)
                        .eq(city != null && StringUtils.isNotBlank(city), "city", city)
                        .eq(code != null && StringUtils.isNotBlank(code), "code", code)
                        .eq(name != null && StringUtils.isNotBlank(name), "name", name)
                        .eq(compId != null && StringUtils.isNotBlank(compId), "comp_id", compId)
                        .eq(compId != null && StringUtils.isNotBlank(type), "type", type)
        );

        List<SeatEntity> seatEntities = page.getRecords();
        List<SeatVo> vos = SeatVo.convertFor(seatEntities);

        List<SeatVo> shareList = new ArrayList<>();
        List<SeatVo> fixedList = new ArrayList<>();

        SysDictQueryParam param = new SysDictQueryParam();
        for (SeatVo vo : vos) {
            param.setCode(vo.getCity());
            param.setName(vo.getProvince());
            param.setType(vo.getCompId());
            SysDictEntity sysDictEntity = sysDictService.queryByNameCodeType(param);
            if (sysDictEntity != null) {
                vo.setCity(sysDictEntity.getValue());
            }
            param.setCode(vo.getProvince());
            param.setName("province");
            param.setType(vo.getCompId());
            sysDictEntity = sysDictService.queryByNameCodeType(param);
            if (sysDictEntity != null) {
                vo.setProvince(sysDictEntity.getValue());
            }

            //共享工位列表
            if ("share".equals(vo.getType())) {
                shareList.add(vo);
            }
            //固定工位列表
            if ("fixed".equals(vo.getType())) {
                fixedList.add(vo);
            }

        }

        SysDictQueryParam dictQueryByNameCodeParam = new SysDictQueryParam();
        dictQueryByNameCodeParam.setCode("seatMap");
        dictQueryByNameCodeParam.setName(compId + "_" + city + "_" + floor);
        SysDictEntity sysDictEntity = sysDictService.queryByNameCode(dictQueryByNameCodeParam);
        String picUrl = sysDictEntity == null ? "" : sysDictEntity.getValue();
//        return new SeatPageUtils(vos, 500, 500, 1, picUrl, shareList, fixedList);
        return new SeatPageUtils(null, 500, 500, 1, picUrl, shareList, fixedList);
    }

    @Override
    public SeatVo queryByCodeAndCompId(String code, String compId) {
        SeatVo vo = SeatVo.convertFor(seatDao.selectOne(new QueryWrapper<SeatEntity>()
                .eq(StringUtils.isNotBlank(code), "code", code)
                .eq(StringUtils.isNotBlank(compId), "comp_id", compId)
        ));

        SysDictQueryParam provQueryParam = new SysDictQueryParam();
        provQueryParam.setName("province");
        provQueryParam.setCode(vo.getProvince());
        provQueryParam.setType(vo.getCompId());
        SysDictEntity provDict = sysDictService.queryByNameCodeType(provQueryParam);
        if (provDict != null) {
            vo.setProvinceName(provDict.getValue());

            SysDictQueryParam cityQueryParam = new SysDictQueryParam();
            cityQueryParam.setName(provDict.getCode());
            cityQueryParam.setCode(vo.getCity());
            cityQueryParam.setType("sys");
            SysDictEntity cityDict = sysDictService.queryByNameCodeType(cityQueryParam);
            if (cityDict != null) {
                vo.setCityName(cityDict.getValue());
            }
        }


        SysDictQueryParam floorQueryParam = new SysDictQueryParam();
        floorQueryParam.setName("floor");
        floorQueryParam.setCode(vo.getFloor());
        floorQueryParam.setType(vo.getCompId());
        SysDictEntity floorDict = sysDictService.queryByNameCodeType(floorQueryParam);
        if (floorDict != null) {
            vo.setFloorName(floorDict.getValue());
        }

        SeatEslEntity seatEslEntity = seatEslService.queryBySeatId(vo.getId());
        if (seatEslEntity != null && StringUtils.isNotBlank(seatEslEntity.getEslId())) {
            vo.setEslId(String.valueOf(IntDexUtils.HtoD(seatEslEntity.getEslId())));
        }

        List<SeatApplicationEntity> seatApplicationEntitys = seatApplicationService.queryBySeatId(vo.getId());
        if (CollectionUtil.isNotEmpty(seatApplicationEntitys)) {
            vo.setDepartment(seatApplicationEntitys.get(0).getDepartment());
            vo.setEmail(seatApplicationEntitys.get(0).getEmail());
        }

        return vo;

    }

}
